<template>
  <PageTemplate :tableRequest="tableRequest" :metaDataRequestParams="metaDataRequestParams"
                :tableSlotNames="tableSlotNames" :title="title" :isShowTableCheckbox="false"
                contentHeight="100vh" @onRowClick="handleRowClick">
    <component v-for="slotItem in tableSlots" :key="slotItem.componentId" :is="slotItem.componentId"
               :slot="slotItem.name" slot-scope="{text,record,resetTableData}" :record="record"
               :type="slotItem.name" :class="slotItem.class" :text="text" :urlParams="urlParamObj"
               :slotData="record" @click.stop="handleSlotItemClick(slotItem, record, resetTableData)"
               @openLink="handleOpenLink">
      {{ slotItem.format ?
        slotItem.format(text) :
        text
      }}</component>
  </PageTemplate>
</template>
<script>
import PageTemplate from '@/components/page-template';
import mixin from '../mixin';
import { getUrlSearchParams, showDiagram } from '@/utils';
import TableCollum from './table-collum';
import CategoryLabel from '@/components/category-label';
import { getParams } from '@/utils';

const portalRequestParams = {
  fragmentId: getUrlSearchParams('fragmentId'),
  ordinal: getUrlSearchParams('ordinal'),
  bigTitleIndex: getUrlSearchParams('bigTitleIndex'),
};

const otherTableArgs = {
  ...portalRequestParams,
  state: '4',
  isTrack: "false",
  section: "doneSection",
  sectionName: "doneSection",
  isGroupBy: "false",
  condition: "comQuery",
  textfield: ""
};

const metaDataRequestParams = {
  key: "LIST_DONE_SECTION",
  platform: 0,
  arguments: {
    ...portalRequestParams,
    userId: window._currentUser.id
  }
};

function linkToDone(app, subApp) {
  if (app === 6) {
    //会议应用
    window.location.href = _ctxPath + "/meeting.do?method=home&list=done" + CsrfGuard.getUrlSurffix();
  } else if (app === 29) {
    //会议室
    window.location.href = _ctxPath + "/meetingroom.do?method=home";
  } else if (app == 4) {
    //表单公文
    if (subApp === 1 || subApp === 19) {
      //G6公文-发文
      window.location.href = _ctxPath + "/govdoc/govdoc.do?method=index&govdocType=1&listType=listDoneAll&_resourceCode=F20_govDocSendManage";
    } else if (subApp === 3 || subApp === 21) {
      //G6公文-签报
      window.location.href = _ctxPath + "/govdoc/govdoc.do?method=index&govdocType=3&listType=listDoneAll&_resourceCode=F20_signReport";
    } else if (subApp === 2 || subApp === 4 || subApp === 20) {
      //G6公文-收文
      window.location.href = _ctxPath + "/govdoc/govdoc.do?method=index&govdocType=2,4&listType=listDoneAll&_resourceCode=F20_receiveManage";
    } else if (subApp === 22) {//G6老公文-交换
    } else if (subApp === 23) {//G6老公文-签收
    } else if (subApp == 24) {//G6老公文-登记
    } else if (subApp == 34) {//G6老公文-分发
    }
  } else if (app == 81) {
    return;
  } else {
    window.location.href = _ctxPath + "/collaboration/collaboration.do?method=listDone" + CsrfGuard.getUrlSurffix();
  }
}

export default {
  name: 'PageFragmentDownList',
  mixins: [mixin],
  components: { PageTemplate, TableCollum, CategoryLabel },
  data() {
    return {
      title: '',
      tableRequest: {
        url: '/ajax.do?method=ajaxAction&managerName=pendingManager',
        managerMethod: 'getMoreList4SectionContion',
        otherArgs: otherTableArgs
      },
      metaDataRequestParams,
      tableSlots: [
        {
          name: 'workitemId',
          componentId: 'TableCollum'
        },
        {
          name: 'subject',
          componentId: 'TableCollum'
        },
        {
          name: 'currentNodesInfo',
          componentId: 'a',
          class: 'color_black'
        },
        {
          name: 'categoryLabel',
          componentId: 'CategoryLabel'
        }
      ],
      urlParamObj: {}
    };
  },
  watch: {
    record() {
      this.content = this.getRenderHtml(this.type, this.record);
    }
  },
  computed: {
    tableSlotNames() {
      return this.tableSlots.map(slot => slot.name);
    }
  },
  methods: {
    handleSlotItemClick(slotItem, { caseId, processId, applicationCategoryKey, activityId, bodyType, applicationSubCategoryKey }, resetTableData) {
      switch (slotItem.name) {
        case 'currentNodesInfo':
          const options = {
            targetWin: getCtpTop(),
            caseId,
            processId,
            isTemplate: false,
            showHastenButton: false,
            appName: applicationCategoryKey,
            currentNodeId: activityId,
            scene: 3,
            canExePrediction: bodyType === "20" && $.ctx.hasPlugin("workflowAdvanced")
          };
          showDiagram(options);
          break;
        case 'categoryLabel':
          linkToDone(applicationCategoryKey, applicationSubCategoryKey, resetTableData);
          break;
      }
    },
    handleOpenLink({ applicationCategoryKey, applicationSubCategoryKey }) {
      linkToDone(applicationCategoryKey, applicationSubCategoryKey);
    }
  },
  created() {
    this.urlParamObj = getParams(window.location.search);
    this.title = this.urlParamObj.name ? (this.urlParamObj.name = window.decodeURIComponent(this.urlParamObj.name))
        : this.$i18n("portal.icons.name230");
  }
};
</script>
